package everyday;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class T1079 {
    public int numTilePossibilities(String tiles) {
        Map<Character, Integer> map = new HashMap<>();
        for (Character c : tiles.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        Set<Character> set = map.keySet();
        return dfs(tiles.length(), map, set)-1;
    }


    public int dfs(int cnt, Map<Character, Integer> map, Set<Character> set) {
        if (cnt == 0) return 1;
        int res = 1;
        for (Character c : set) {
            int d=map.get(c);
            if (d > 0) {
                map.put(c, d - 1);
                res += dfs(cnt - 1, map, set);
                map.put(c, d);
            }
        }
        return res;
    }
}
